#
# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
# Output: 7 -> 0 -> 8
# Explanation: 342 + 465 = 807.
#
class ListNode:
    def __init__(self, val, next=None):
        self.val = val
        self.next = next

    def __repr__(self):
        return str(self.val)+"->"+repr(self.next)

empty = ListNode(0)

def add(a, b, carry = 0):
    if not a and not b and not carry:
        return None
    a = a if a else empty
    b = b if b else empty

    val = a.val + b.val + carry

    cur = ListNode(val % 10, add(a.next, b.next, val//10))

    return cur

a1 = ListNode(2)
a2 = ListNode(4)
a3 = ListNode(3)

a1.next = a2
a2.next = a3

b1 = ListNode(5)
b2 = ListNode(6)
b3 = ListNode(9)

b1.next = b2
b2.next = b3

result = add(a1,b1)
print(result)